<?php
# bitmap 案例

//日期对应的活跃用户
$data = [
    '2017-01-10' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    '2017-01-11' => array(1, 2, 3, 4, 5, 6, 7, 8),
    '2017-01-12' => array(1, 2, 3, 4, 5, 6),
    '2017-01-13' => array(1, 2, 3, 4),
    '2017-01-14' => array(1, 2)
];

//批量设置活跃状态 
foreach ($data as $date => $uids) {
    $cacheKey = sprintf("stat_%s", $date);
    foreach ($uids as $uid) {
        $redis->setBit($cacheKey, $uid, 1);
    }
}

$redis->bitOp('AND', 'stat', 'stat_2017-01-10', 'stat_2017-01-11', 'stat_2017-01-12') . PHP_EOL;

//总活跃用户：6 
echo "总活跃用户：" . $redis->bitCount('stat') . PHP_EOL;

$redis->bitOp('AND', 'stat1', 'stat_2017-01-10', 'stat_2017-01-11', 'stat_2017-01-14') . PHP_EOL;
//总活跃用户：2 
echo "总活跃用户：" . $redis->bitCount('stat1') . PHP_EOL;

$redis->bitOp('AND', 'stat2', 'stat_2017-01-10', 'stat_2017-01-11') . PHP_EOL;
//总活跃用户：8 
echo "总活跃用户：" . $redis->bitCount('stat2') . PHP_EOL;